import 'package:dio/dio.dart';

import 'YZHttp.dart';

class HttpUtils {
  static void init({
    required String baseUrl,
    Map<String, String>? headers,
    int connectTimeout = CONNECT_TIMEOUT,
    int receiveTimeout = RECEIVE_TIMEOUT,
    List<Interceptor>? interceptors,
  }) {
    YZHttp().init(
      baseUrl: baseUrl,
      headers: headers,
      connectTimeout: connectTimeout,
      receiveTimeout: receiveTimeout,
      interceptors: interceptors,
    );
  }

  static void cancelRequests({required CancelToken token}) {
    YZHttp().cancelRequests(token: token);
  }

  static void get(String path,
      {Map<String, dynamic>? params,
      Options? options,
      CancelToken? cancelToken,
      bool refresh = false,
      bool noCache = !CACHE_ENABLE,
      String? cacheKey,
      bool cacheDisk = false,
      required HttpSuccessCallBack successCallBack,
      required HttpErrorCallBack errorCallBack}) async {
    YZHttp().get(path,
        params: params,
        options: options,
        cancelToken: cancelToken,
        refresh: refresh,
        noCache: noCache,
        cacheKey: cacheKey,
        successCallBack: successCallBack,
        errorCallBack: errorCallBack);
  }

  static void post(String path,
      {data,
      Map<String, dynamic>? params,
      Options? options,
      CancelToken? cancelToken,
      required HttpSuccessCallBack successCallBack,
      required HttpErrorCallBack errorCallBack}) async {
    YZHttp().post(path,
        data: data,
        params: params,
        options: options,
        cancelToken: cancelToken,
        successCallBack: successCallBack,
        errorCallBack: errorCallBack);
  }

  /// 上传
  /// FormData data 例子：
  /// var formData = FormData.fromMap({
  ///   'name': 'wendux',
  ///   'age': 25,
  ///   'file': await MultipartFile.fromFile('./text.txt', filename: 'upload.txt'),
  ///   'files': [
  ///     await MultipartFile.fromFile('./text1.txt', filename: 'text1.txt'),
  ///     await MultipartFile.fromFile('./text2.txt', filename: 'text2.txt'),
  ///   ]
  /// });
  static void upload(String path, FormData data,
      {Map<String, dynamic>? params,
      Options? options,
      CancelToken? cancelToken,
      required void Function(int, int)? onSendProgress,
      required HttpSuccessCallBack successCallBack,
      required HttpErrorCallBack errorCallBack}) async {
    YZHttp().upload(path, data,
        params: params,
        cancelToken: cancelToken,
        onSendProgress: onSendProgress,
        successCallBack: successCallBack,
        errorCallBack: errorCallBack);
  }

  /// 上传
  /// Stream
  static void uploadStream(String path, List<int> postData,
      {Map<String, dynamic>? params,
      Options? options,
      CancelToken? cancelToken,
      required void Function(int, int)? onSendProgress,
      required HttpSuccessCallBack successCallBack,
      required HttpErrorCallBack errorCallBack}) async {
    YZHttp().uploadStream(path, postData,
        params: params,
        cancelToken: cancelToken,
        onSendProgress: onSendProgress,
        successCallBack: successCallBack,
        errorCallBack: errorCallBack);
  }
}
